package com.tz.web.weixin;

import com.tz.bean.Good;
import com.tz.bean.TzParams;
import com.tz.bean.User;
import com.tz.service.good.IGoodService;
import com.tz.service.user.IUserService;
import com.tz.util.TmStringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商城主页Controller
 */
@Controller
@RequestMapping("/weixin")
public class WeiXinIndexController {

    @Resource
    private IGoodService goodService;

    @Resource
    private IUserService userService;


    @RequestMapping("/login")
    public String login(TzParams params , Model model){
        return "weixin/login1";
    }

    @RequestMapping("/register")
    public String regiister(TzParams params , Model model){
        System.out.println(9);
        return "weixin/register";
    }


    @RequestMapping("/shop")
    public String shop(TzParams params , Model model){
        List<Good> goods =  goodService.findGoods(params);
        model.addAttribute("goods",goods);
         //查询所有商品并分类展示
         return "weixin/shop";
    }

    @RequestMapping("/category")
    public String category(){
        System.out.println(555);
        return "weixin/category";
    }

    @RequestMapping("/shoppingchart")
    public String shoppingchart(){
        System.out.println(555);
        return "weixin/shoppingchart";
    }

    @RequestMapping("/member")
    public String member(){
        System.out.println(555);
        return "weixin/member";
    }

    @ResponseBody
    @RequestMapping(value="/logined",method= RequestMethod.POST)
    public Map logined(TzParams params){
        Map<String ,Object> map = new HashMap<>();
        if(params!=null){
            if(TmStringUtils.isNotEmpty(params.getPhone()) && TmStringUtils.isNotEmpty(params.getPassword())){
                String ss = TmStringUtils.md5Base64(params.getPassword());
                params.setPassword(TmStringUtils.md5Base64(params.getPassword()));
                User user = userService.getLogin(params);
                if(user!=null){
                    if(TmStringUtils.isEmpty(user.getForbidencode()) || "002".equals(user.getForbidencode())){
                        map.put("code","forbiden");
                    }else{
                        map.put("code","success");
                        map.put("user",user);
                    }
                }else{
                    map.put("code","fail");
                }
            }else{
                map.put("code","null");//请输入账号和密码
            }
        }else{
            map.put("error","null");
        }
        return map;
    }

//    @ResponseBody
//    @RequestMapping(value="/register",method= RequestMethod.POST)
//    public Map register(TzParams params){
//
//    }





}
